// import PREFIX from '@/router/prefix.js';
import store from '@/store';

// 判断是否是首页或者登录页面
function checkIsLoginPage(){
    // return whiteList.some(item => {
    //     let temp = PREFIX + item;
    //     return temp.includes(window.location.pathname);
    // })
}

async function getContext() {
    // 判断是否是白名单
    // let isLoginPage = checkIsLoginPage.call(this);
    // if(!isLoginPage){
    //     store.dispatch('checkIsLogout');
    // }
    try {
        await getLatestVerCode();
    }catch(err) {
        console.log(err);
    }
}

// 品牌
const DEFAULT_BRAND = process.env.VUE_APP_BRAND;

// 获取品牌版本信息
function getLatestVerCode() {
    return new Promise((resolve, reject) => {
        G_transmit('getLatestVerCode', {
            queries: {
                brand: DEFAULT_BRAND
            }
        }).then(res => {
            let { data } = res;
            if(data && data.brandState === 1) {
                getLatestBrand(data.ver).then(result => {
                    store.dispatch('updateBrand', result);
                    store.dispatch('setSiteInfo', result);
                    resolve();
                }).catch(err => {
                    reject(new Error(err));
                });
            }
        }).catch(err => {
            console.log(err);
        });
    });
} 
    

// 获取最新品牌信息
function getLatestBrand(ver) {
    let brand = store.state.brand;
    return new Promise((resolve, reject) => {
        if(ver != brand.ver || !brand.publishBrand) {
            G_transmit('getLatestBrand', {
                queries: {
                    brand: DEFAULT_BRAND
                } 
            }).then(res => {
                resolve(res.data);
            }).catch(err => {
                console.log('err = ', err)
            });
        } else {
            resolve(brand);
        }
    })
}

export {
    getContext,
    checkIsLoginPage,
    getLatestVerCode
}
